package gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import files.FileTreeModel;
import files.ImageFinder;

public class GUI {
	public static String[] types = {"jpg"};;

	public void splash() {
		JFrame frame = new JFrame("Duplicate Image Finder");
		frame.setLayout(new BorderLayout());
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final JTextField path = new JTextField("No directory selected.");
		path.setEditable(false);
		
		JButton choose = new JButton("Choose");
		choose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new File("."));
				chooser.setDialogTitle("Choose directory to scan");
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.setAcceptAllFileFilterUsed(false);
				
				if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					path.setText(chooser.getSelectedFile().getPath());
				}
			}
		});
		
		Container cont = new Container();
		cont.setLayout(new GridLayout(2,2));
		
		cont.add(new JLabel("Choose directory"));		
		cont.add(choose);
		cont.add(new JLabel("Directory:"));
		cont.add(path);
		
		Container top = new Container();
		top.setLayout(new BorderLayout());
		
		top.add(cont, BorderLayout.NORTH);
		
		JButton scan = new JButton("Scan for images");
		final DefaultListModel list_model = new DefaultListModel();
		JList list = new JList(list_model);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setLayoutOrientation(JList.VERTICAL);
		list.setVisibleRowCount(10);
		
		
		JScrollPane listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(250,80));

		final JTextArea output = new JTextArea(10,30);
		scan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				list_model.clear();
				try {
					ImageFinder im = new ImageFinder(path.getText(),types);
					File[] files = im.search();
					for(int i = 0; i < files.length; i++) {
						list_model.addElement(files[i]);
					}
				} catch (NullPointerException e1) {}
			}
		});
		
		list.addListSelectionListener(new FileListSelectionListener());
		
		final JTree tree = new JTree(new DefaultTreeModel(new DefaultMutableTreeNode()));
		
		scan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					FileTreeModel model;
					model = new FileTreeModel(new File(path.getText()),types);
					tree.setModel(model);
				} catch (NullPointerException e1) {}
			}
		});
		
		JScrollPane scrollpane = new JScrollPane(tree);
	    
		JScrollPane scroll = new JScrollPane(output);
		scroll.setPreferredSize(new Dimension(450,110));

		top.add(scan, BorderLayout.SOUTH);
		frame.add(top, BorderLayout.NORTH);
		frame.add(scrollpane, BorderLayout.CENTER);
		
		frame.pack();
		
		frame.setVisible(true);
	}
}
